# _*_coding=utf-8 _*_
"""
@author :   xh
@date   :   2023/11/1 10:14
"""
import re
from mathsolver.functions.base.objects import BaseComplexPoly, BaseComplexEq
from mathsolver.SyntacticParser.parser import auto_parser


def get_expression_ahfs0001(raw):
    params = raw.get("params", {})
    expression = params.get("expression", [])
    polys = []
    for item in expression:
        code, t = auto_parser(item)
        if code == 1:
            exprs = t["exprs"]
            lats = t["latex"]
            for key, value in exprs.items():
                if re.sub('[0-9]+', '', key) == 'poly':
                    item = BaseComplexPoly(value)
                    item.add_latex(lats[key])
                    polys.append(item)
        else:
            raise Exception("parser-failed: %s" % str(code))
    assert polys
    return polys


def get_expression_ahfs0002(raw):
    params = raw.get("params", {})
    equation = params.get('equation', [])
    eqs = []
    for item in equation:
        code, t = auto_parser(item)
        if code == 1:
            exprs = t["exprs"]
            lats = t["latex"]
            for key, value in exprs.items():
                if re.sub('[0-9]+', '', key) == 'eq':
                    item = BaseComplexEq(value)
                    item.add_latex(lats[key])
                    eqs.append(item)
        else:
            raise Exception("parser-failed: %s" % str(code))
    assert eqs
    return eqs
